package com.partvoyage.client.outil;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.partvoyage.client.service.infopoint.InfoPointService;
import com.partvoyage.client.service.infopoint.InfoPointServiceAsync;
import com.partvoyage.shared.InfoPointBean;

/**
 * A composite of a TextBox and a CheckBox that optionally enables it.
 */
public class OptionalTextBox extends Composite implements ClickHandler {

	private TextBox textBox = new TextBox();
	private CheckBox checkBox = new CheckBox();

	/**
	 * Constructs an OptionalTextBox with the given caption on the check.
	 * 
	 * @param caption
	 *            the caption to be displayed with the check box
	 */
	public OptionalTextBox(String caption) {
		// Place the check above the text box using a vertical panel.
		VerticalPanel panel = new VerticalPanel();
		panel.add(checkBox);
		panel.add(textBox);

		// Set the check box's caption, and check it by default.
		checkBox.setText(caption);
		checkBox.setChecked(true);
		checkBox.addClickHandler(this);

		// All composites must call initWidget() in their constructors.
		initWidget(panel);

		// Give the overall composite a style name.
		setStyleName("example-OptionalCheckBox");
	}

	private final InfoPointServiceAsync infoBulleMarkerService = GWT
			.create(InfoPointService.class);

	public void onClick(ClickEvent event) {
		Object sender = event.getSource();
		if (sender == checkBox) {
			// When the check box is clicked, update the text box's enabled
			// state.
			textBox.setEnabled(checkBox.isChecked());

			GWT.log(textBox.getText());
		}
//		infoBulleMarkerService.create(textBox.getText(),
//				new AsyncCallback<InfoBulleMarkerBean>() {
//
//					public void onFailure(Throwable caught) {
//						Window.alert("error during getting users");
//					}
//
//					@SuppressWarnings("deprecation")
//					public void onSuccess(InfoBulleMarkerBean result) {
//						infoBulleMarkerService
//								.findAll(new AsyncCallback<List<InfoBulleMarkerBean>>() {
//
//									public void onFailure(Throwable caught) {
//										Window.alert("error during getting users");
//									}
//
//									@SuppressWarnings("deprecation")
//									public void onSuccess(
//											InfoBulleMarkerBean result) {
//
//									}
//
//									@Override
//									public void onSuccess(
//											List<InfoBulleMarkerBean> result) {
//										for (InfoBulleMarkerBean dataInfoBulleMarker : result) {
//											GWT.log(dataInfoBulleMarker
//													.getText());
//										}
//
//									}
//								});
//					}
//				});
	}

	/**
	 * Sets the caption associated with the check box.
	 * 
	 * @param caption
	 *            the check box's caption
	 */
	public void setCaption(String caption) {
		// Note how we use the use composition of the contained widgets to
		// provide
		// only the methods that we want to.
		checkBox.setText(caption);
	}

	/**
	 * Gets the caption associated with the check box.
	 * 
	 * @return the check box's caption
	 */
	public String getCaption() {
		return checkBox.getText();
	}
}
